# Sports controller controls display of sport matches
# In 13wins, each tab displayed a certain sport. Each sport allows for a variety of different filters based on user interaction and match attributes

class SportsController < ApplicationController
  # Display Sport 
  def show
    flash[:notice]= params[:noice] if params[:notice] and !flash[:notice]
    flash[:error]= params[:error] if params[:error] and !flash[:error]
    @sport = Sport.find params[:id]
    @league = League.find_by_id(params[:league_id])
    # exclude matches that have been bet on

    @matches = case params[:order_by]
    when "comment_count"
      limit = SETTINGS["sport.filter.most_comment.limit"] || 6
      @matches = current_user.matches.friends_scope.normal.except_placed(current_user).scoped(:order=>"#{Match.table_name}.comment_count DESC")
	@matches = @matches.paginate :per_page=>limit,:page=>params[:page],
                                    :include=>[:home_team,:away_team,:league],
                                    :conditions=>["#{Match.table_name}.sport_id = ?",@sport.id]

    when "most_bets"
      limit = SETTINGS["sport.filter.most_bets.limit"] || 6
      @matches = current_user.matches.friends_scope.normal.except_placed(current_user).scoped(:order =>"friends_count DESC").paginate :page=>params[:page], 
	:per_page=>limit,
	#:include=>[:home_team,:away_team,:league],        
	:conditions=>["#{Match.table_name}.sport_id = ?",@sport.id]
  

    when "most_popular"
      limit = SETTINGS["sport.filter.most_popular.limit"] || 6
      @matches = current_user.matches.friends_scope.scoped(:order=>"friends_count DESC, #{Match.table_name}.comment_count DESC").normal.except_placed(current_user).paginate :per_page=>limit,
        :page=>params[:page],
	#:include=>[:home_team,:away_team,:league],    
	:conditions=>["#{Match.table_name}.sport_id = ?",@sport.id]
        

    else
      # normal
      @matches = @sport.matches.scoped(:order=>"#{Match.table_name}.begin_at ASC,#{Match.table_name}.id ASC")
      @matches = @matches.scoped(:conditions=>{:league_id=>@league.id}) if @league
      @matches = @matches.normal.except_placed(current_user).paginate :page=>params[:page]
    end


  end
end
